package com.tkuniyoshi.battleshipgame.common.log;

import com.tkuniyoshi.battleshipgame.common.log.intf.LogMessage;
import android.util.Log;

public class BattleShipLogger {
	private static BattleShipLogger logger = null;
	private String AppTag;
	
	private BattleShipLogger(String Tag){
		AppTag = Tag;
	}
	
	public BattleShipLogger initialize(String AppTag){
		if(logger == null){
			logger = new BattleShipLogger(AppTag);
		}
		
		return logger;
	}
	
	public static BattleShipLogger getInstance(){
		return BattleShipLogger.logger;
	}
	
	public void log(LogMessage logMessage){
		if(logMessage.getLogLevel().equals("E")){
			Log.e(AppTag, this.getLogString(logMessage));
		}else if(logMessage.getLogLevel().equals("W")){
			Log.w(AppTag, this.getLogString(logMessage));
		}else if(logMessage.getLogLevel().equals("I")){
			Log.i(AppTag, this.getLogString(logMessage));
		}else if(logMessage.getLogLevel().equals("D")){
			Log.d(AppTag, this.getLogString(logMessage));
		}else if(logMessage.getLogLevel().equals("V")){
			Log.v(AppTag, this.getLogString(logMessage));
		}
	}
	
	public void log(LogMessage logMessage,Throwable throwable){
		if(logMessage.getLogLevel().equals("E")){
			Log.e(AppTag, this.getLogString(logMessage),throwable);
		}else if(logMessage.getLogLevel().equals("W")){
			Log.w(AppTag, this.getLogString(logMessage),throwable);
		}else if(logMessage.getLogLevel().equals("I")){
			Log.i(AppTag, this.getLogString(logMessage),throwable);
		}else if(logMessage.getLogLevel().equals("D")){
			Log.d(AppTag, this.getLogString(logMessage),throwable);
		}else if(logMessage.getLogLevel().equals("V")){
			Log.v(AppTag, this.getLogString(logMessage),throwable);
		}
	}
	
	public void log(LogMessage logMessage,Object[] obj,Throwable throwable){
		if(logMessage.getLogLevel().equals("E")){
			Log.e(AppTag, this.getLogString(logMessage),throwable);
		}else if(logMessage.getLogLevel().equals("W")){
			Log.w(AppTag, this.getLogString(logMessage),throwable);
		}else if(logMessage.getLogLevel().equals("I")){
			Log.i(AppTag, this.getLogString(logMessage),throwable);
		}else if(logMessage.getLogLevel().equals("D")){
			Log.d(AppTag, this.getLogString(logMessage),throwable);
		}else if(logMessage.getLogLevel().equals("V")){
			Log.v(AppTag, this.getLogString(logMessage),throwable);
		}
	}
	
	private String getLogString(LogMessage logMessage){
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(logMessage.getLogLevel());
		stringBuffer.append(logMessage.getLogMessageID());
		stringBuffer.append(" - ");
		stringBuffer.append(logMessage.getLogMessage());
		
		return stringBuffer.toString();
	}
}
